﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component.DataFactory;
using SyCODE.Component;
using System.Xml;
using System.IO;
using System.Data.Common;

namespace SyCODE.Blog
{
    /// <summary>
    /// GetFriend 的摘要说明
    /// </summary>
    public class GetFriend : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            NameValueCollection nvc = new NameValueCollection(context.Request.QueryString);
            DBAM dbam = new DBAM();
            try
            {
                dbam.BuildParameters("myfriend", nvc);

                context.Response.Expires = 120;
                context.Response.ContentType = "text/xml";

                XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);

                writer.WriteStartDocument();
                writer.WriteStartElement("html");

                writer.WriteStartElement("head");

                writer.WriteStartElement("meta");
                writer.WriteAttributeString("http-equiv", "Content-Type");
                writer.WriteAttributeString("content", "text/html; charset=" + context.Response.ContentEncoding.WebName);
                writer.WriteEndElement();

                writer.WriteStartElement("title");
                writer.WriteString(String.Format(StringResource.GetField("friendtitle"), nvc["u"]) + " - Powered By X3BLOG");
                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteStartElement("body");

                DbDataReader dr = dbam.ExecuteReader();
                while (dr.Read())
                {
                    writer.WriteStartElement("a");
                    writer.WriteAttributeString("id", dr.GetString(0));
                    writer.WriteAttributeString("href", "/" + dr.GetString(0));
                    writer.WriteString(dr.GetString(1));
                    writer.WriteEndElement();
                }
                dr.Close();

                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
            finally
            {
                dbam.Dispose();
            }
        }

        #endregion
    }
}